package com.lyshop.jinding.activitys;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.lyshop.jinding.R;
import com.lyshop.jinding.application.MyApplication;
import com.lyshop.jinding.base.BaseActivity;
import com.lyshop.jinding.bean.TakeMoneyTipsBean;
import com.lyshop.jinding.bean.UserBalanceBean;
import com.lyshop.jinding.bean.UserBankCardBean;
import com.lyshop.jinding.bean.UserInfoBean;
import com.lyshop.jinding.constants.Constant;
import com.lyshop.jinding.core.AppAction;
import com.lyshop.jinding.net.ActionCallbackListener;
import com.lyshop.jinding.utils.CustomerUtil;
import com.lyshop.jinding.utils.DataUtils;
import com.lyshop.jinding.utils.RouteUtil;
import com.lyshop.jinding.utils.TimeCount;
import com.lyshop.jinding.utils.ToastUtil;
import com.lyshop.jinding.view.TakeMoneyRuleDialog;
import com.lyshop.jinding.view.UnBindWithdrawCardDialog;


public class TakeMoneyActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "TakeMoneyActivity";
    private TextView cardNo, allDraw, enableBalance, takeGetCode, moreQuestion, withdrawDesc, tv_money_can_draw, tv_card_text;
    private UserBankCardBean cardData;
    private EditText money_number, code_number;
    private Button submitBtn;
    private RelativeLayout rl_card_no;
    int min_m, max_m;
    private ImageView iv_right;
    UnBindWithdrawCardDialog unBindWithdrawCardDialog;
    private TakeMoneyRuleDialog takeMoneyRuleDialog;
    private TimeCount timeCount;

    protected int setContentView() {
        return R.layout.activity_take_money;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getCardList();
        getViewData();
    }

    @Override
    protected void findViews() {
        cardNo = (TextView) $(R.id.tv_card_no);
        allDraw = (TextView) $(R.id.tv_all_with_draw);
        money_number = (EditText) $(R.id.et_sum_money);
        enableBalance = (TextView) $(R.id.tv_enable_balance);
        takeGetCode = (TextView) $(R.id.tv_get_code);
        moreQuestion = (TextView) $(R.id.tv_more_question);
        code_number = (EditText) $(R.id.et_code);
        submitBtn = (Button) $(R.id.btn_submit);
        withdrawDesc = (TextView) $(R.id.tv_withdraw_desc);
        tv_money_can_draw = (TextView) $(R.id.tv_money_can_draw);
        tv_card_text = (TextView) $(R.id.tv_card_text);
        rl_card_no = (RelativeLayout) $(R.id.rl_card_no);
        iv_right = (ImageView) $(R.id.iv_right);
    }

    @Override
    protected void setListeners() {
        rl_card_no.setOnClickListener(this);
        allDraw.setOnClickListener(this);
        takeGetCode.setOnClickListener(this);
        moreQuestion.setOnClickListener(this);
        submitBtn.setOnClickListener(this);
        iv_right.setOnClickListener(this);

    }

    private void getViewData() {
        AppAction mAppAction = MyApplication.getApplication().getAppAction();
        mAppAction.takeMoneyTips(new ActionCallbackListener<TakeMoneyTipsBean>() {
            @Override
            public void onSuccess(TakeMoneyTipsBean data) {
                Log.e(TAG, "onSuccess: ." + data.getContent());
                withdrawDesc.setText(data.getContent());
                tv_money_can_draw.setText("(" + data.getMoney() + "元以上起提)");
                min_m = data.getMoney();
                max_m = data.getMax_money();

                if (!DataUtils.getFirstTakeMoney(mContext)) {
                    showTakeMoneyRuleDialog(data.getContent());
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext, message);
            }
        });
        mAppAction.getUserBalance(new ActionCallbackListener<UserBalanceBean>() {
            @Override
            public void onSuccess(UserBalanceBean data) {
                enableBalance.setText("可转出金额为" + data.can_withdraw_balance + "元,总金额为" + data.available_balance + "元");
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    private void showTakeMoneyRuleDialog(String rule) {
        if (takeMoneyRuleDialog == null) {
            takeMoneyRuleDialog = new TakeMoneyRuleDialog(mContext, rule);
        }
        if (!takeMoneyRuleDialog.isShowing()) {
            takeMoneyRuleDialog.show();
        }
    }

    private void getCardList() {
        mAppAction.getCardList("1", new ActionCallbackListener<UserBankCardBean>() {
            @Override
            public void onSuccess(UserBankCardBean data) {

                if (null == data || TextUtils.isEmpty(data.card_no)) {
                    cardNo.setText("添加银行卡");
                    tv_card_text.setText("");
                } else {
                    cardData = data;
                    String cardNumber = cardData.card_no.substring(cardData.card_no.length() - 4, cardData.card_no.length());
                    cardNo.setText("银行卡(" + cardNumber + ")");
                    tv_card_text.setText("解绑");
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    private void takeMoneyGetCode() {
        loading();
        mAppAction.takeMoneyGetCode(new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                cancelLoading();
                timeCount = new TimeCount(takeGetCode, 60000, 1000, "再次获取");
                timeCount.start();
                ToastUtil.showToast(mContext, "验证码发送成功");
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext, message);

            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.rl_card_no) {
            if (null == cardData || TextUtils.isEmpty(cardData.card_no)) {//添加
                Log.e(TAG, "onClick: " + "绑卡");
            } else {//解
                Log.e(TAG, "onClick: " + "解绑");
                if (null == unBindWithdrawCardDialog) {
                    unBindWithdrawCardDialog = new UnBindWithdrawCardDialog(mContext, true, Gravity.CENTER);
                }
                if (!unBindWithdrawCardDialog.isShowing()) {
                    unBindWithdrawCardDialog.show();
                }
            }
            return;
        }
        if (view.getId() == R.id.tv_all_with_draw) {//全部提现
            UserInfoBean bean = DataUtils.getUserInfo(mContext);
            money_number.setText(bean.can_withdraw_balance + "");
            return;
        }
        if (view.getId() == R.id.tv_get_code) {
//            if (money_number.getText().length() == 0) {
//                ToastUtil.showToast(mContext, "请输入提现金额");
//                return;
//            }
//            takeMoneyGetCode();

            RouteUtil.tosetPayPwdActivity(this);
            return;
        }
        if (view.getId() == R.id.tv_more_question) {
            RouteUtil.toWebViewActivity(TakeMoneyActivity.this, Constant.H5_WITHDRAWAL_RULE);
            return;
        }
        if (view.getId() == R.id.btn_submit) {
            submitMoney();
            return;
        }
        if (view.getId() == R.id.iv_right) {
            CustomerUtil.customerService(mContext);
            return;
        }
    }

    private void submitMoney() {
        if (money_number.getText().length() == 0) {
            ToastUtil.showToast(mContext, "请输入提现金额");
            return;
        }
        double cur_money = Double.valueOf(money_number.getText().toString());
        if (cur_money < min_m || cur_money > max_m) {
            ToastUtil.showToast(mContext, "单笔可转出最小金额为" + min_m + "元," + "最大金额为" + max_m + "元");
            return;
        }
        if (code_number.getText().length() == 0) {
            ToastUtil.showToast(mContext, "请输入提现验证码");
            return;
        }
        mAppAction.takeMoneySubmit(money_number.getText().toString(), "1", code_number.getText().toString(), new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                ToastUtil.showToast(mContext, "提现成功");
                finish();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext, message);
            }
        });
    }
}
